﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WorkflowDesigner
{
    /// <summary>
    /// 图形基类
    /// </summary>
    [DesignTimeVisible(false)]
    public class Shape : Panel
    {
        private Brush m_Brush;
        private Pen m_ScopePen;

        private string _text = string.Empty;
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Bindable(true)]
        [Browsable(true)]
        public new string Text
        {
            get { return _text; }
            set
            {
                if (_text != value)
                {
                    _text = value;
                    EventHandler textChanged = TextChanged;
                    if (textChanged != null)
                    {
                        textChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        public new event EventHandler TextChanged;

        private Pen m_Pen;
        /// <summary>
        /// 画笔
        /// </summary>
        public Pen Pen
        {
            get { return m_Pen; }
        }
        private Rectangle _ClipRectangle;
        /// <summary>
        /// 当前图形
        /// </summary>
        public Rectangle ClipRectangle
        {
            get { return _ClipRectangle; }
            protected set { _ClipRectangle = value; }
        }
        private bool _IsSelected = false;
        /// <summary>
        /// 是否选中状态
        /// </summary>
        public bool IsSelected
        {
            get { return this._IsSelected; }
            set
            {
                if (_IsSelected != value)
                {
                    _IsSelected = value;
                    this.Invalidate();
                }
            }
        }
        /// <summary>
        /// 惟一标识
        /// </summary>
        public int UniqueId
        {
            get
            {
                return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(this);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Form GetTopForm()
        {
            Control parent = this.Parent;
            while (parent != null)
            {
                if (parent is Form)
                {
                    return parent as Form;
                }
                else
                {
                    parent = parent.Parent;
                }
            }
            return null;
        }

        private Shape()
        {
            this.MinimumSize = new Size(80, 70);
            this.Size = this.MinimumSize;
            this.MaximumSize = this.Size;

            m_Pen = (Pen)Pens.Black.Clone();
            m_Brush = Brushes.Blue;

            m_ScopePen = new Pen(Color.Blue, 0.5f);
            m_ScopePen.DashStyle = DashStyle.Dot;

            IsSelected = false;

            this._ClipRectangle = new Rectangle(this.Left, this.Top, (int)(this.Width - this.Pen.Width), (int)(this.Height - this.Pen.Width));

            InitEvents();
        }

        private FlowDesinger _FlowDesinger = null;
        public Shape(FlowDesinger desinger)
            : this()
        {
            _FlowDesinger = desinger;
        }

        private void InitEvents()
        {
            this.MouseDown += new MouseEventHandler(Shape_MouseDown);
            this.MouseLeave += new EventHandler(Shape_MouseLeave);
            this.MouseUp += new MouseEventHandler(Shape_MouseUp);
            this.MouseMove += new MouseEventHandler(Shape_MouseMove);
        }

        void Shape_MouseMove(object sender, MouseEventArgs e)
        {
            Control lCtrl = (sender as Control);
            if (e.Button == MouseButtons.Left)
            {
                lCtrl.Left = lCtrl.Left + e.X - p.X;
                lCtrl.Top = lCtrl.Top + e.Y - p.Y;
                _FlowDesinger.WorkSpace.Refresh();
            }
        }

        void Shape_MouseUp(object sender, MouseEventArgs e)
        {
            Control lCtrl = (sender as Control);
            if (lCtrl.Left <= 0) lCtrl.Left = 1;

            if (lCtrl.Top <= 0) lCtrl.Top = 1;

        }

        void Shape_MouseLeave(object sender, EventArgs e)
        {
            ((Control)sender).Cursor = Cursors.Arrow;
        }

        private Point p, p1;
        void Shape_MouseDown(object sender, MouseEventArgs e)
        {
            p.X = e.X;
            p.Y = e.Y;
            p1.X = e.X;
            p1.Y = e.Y;

            foreach (var item in this._FlowDesinger.FlowNodes)
            {
                if (item.UniqueId == this.UniqueId)
                    item.IsSelected = true;
                else
                    item.IsSelected = false;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {

            base.OnPaint(e);

            DrawHandlers(e.Graphics);
        }

        private void DrawHandlers(Graphics g)
        {
            if (IsSelected)
            {
                g.DrawRectangle(this.m_ScopePen, this._ClipRectangle);

                //左上
                g.FillRectangle(m_Brush, this._ClipRectangle.Left, this._ClipRectangle.Top, 4, 4);
                //左中
                g.FillRectangle(m_Brush, this._ClipRectangle.Left, this._ClipRectangle.Top + this.Height / 2 - 2, 4, 4);
                //左下
                g.FillRectangle(m_Brush, this._ClipRectangle.Left, this._ClipRectangle.Bottom - 4, 4, 4);
                //右上
                g.FillRectangle(m_Brush, this._ClipRectangle.Right - 4, this._ClipRectangle.Top, 4, 4);
                //右中
                g.FillRectangle(m_Brush, this._ClipRectangle.Right - 4, this._ClipRectangle.Top + this._ClipRectangle.Height / 2 - 2, 4, 4);
                //右下
                g.FillRectangle(m_Brush, this._ClipRectangle.Right - 4, this._ClipRectangle.Bottom - 4, 4, 4);
                //上中
                g.FillRectangle(m_Brush, this._ClipRectangle.Left + this._ClipRectangle.Width / 2 - 2, this._ClipRectangle.Top, 4, 4);
                //下中
                g.FillRectangle(m_Brush, this._ClipRectangle.Left + this._ClipRectangle.Width / 2 - 2, this._ClipRectangle.Bottom - 4, 4, 4);
            }
        }
    }
}
